package com.yhzhou.test;

/**
 * Created by root on 17-5-11.
 */
import java.io.*;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer;
import org.deeplearning4j.models.word2vec.Word2Vec;
import org.deeplearning4j.text.sentenceiterator.BasicLineIterator;
import org.deeplearning4j.text.sentenceiterator.SentenceIterator;
import org.deeplearning4j.text.tokenization.tokenizer.preprocessor.CommonPreprocessor;
import org.deeplearning4j.text.tokenization.tokenizerfactory.DefaultTokenizerFactory;
import org.deeplearning4j.text.tokenization.tokenizerfactory.TokenizerFactory;

//import mo.edu.must.perdict.preprocess.Word2VecProcessor;

public class test {
    public static Word2Vec vec;
    private static HashMap<String, Integer> id2Name;
    public static String lasts;
    public static int index;
    public static void main(String[] args) {
        String res,src,vector;
        src = "/root/IdeaProjects/finish/data/tan-data.txt";
        vector = "/root/IdeaProjects/finish/word2vec/vector.txt";

        getLasts();
        //处理数据成我们需要的数据格式
        res = dealData(src);

        //lastApp = "com.google.android.gms";
        try {
            //生成词向量
            vector = computeWord2VecAndComputeSimilarity(res);
            res = dealResult(vector);
            res = dealResult2(res);
            iknnm(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
        getPredictApp();
    }

    public static String[] main2(String appName) {
        String res,src,vector;
        src = "/root/IdeaProjects/finish/data/tan-data.txt";
        vector = "/root/IdeaProjects/finish/word2vec/vector.txt";

        getLasts();
        //处理数据成我们需要的数据格式
        res = dealData(src);

        //lastApp = "com.google.android.gms";
        try {
            //生成词向量
            vector = computeWord2VecAndComputeSimilarity(res);
            res = dealResult(vector);
            res = dealResult2(res);
            iknnm(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return getPredictApp();
    }

    public static void getLasts(){
        lasts = "com.tencent.mobileqq+true+home+false+home+null+null+true+com.tencent.mm";

    }
    /**
     * 用于处理作为数据的预处理，把文件的每行的8个last处理成一个词，最后放进另一个文件里面。
     * @param src 源文件路径
     * @return 处理后文件路径
     */
    public static String dealData(String src) {
        String str=null;
        //8个last的文件
        File file = new File(src);
        StringBuffer word = new StringBuffer("");
        String resource = "/root/IdeaProjects/finish/word2vec/tan-data2.txt";
        FileWriter fw = null;
        //清空文件
        File d = new File(resource);
        if(d.exists())
            d.delete();

        try{

            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            while((str=br.readLine())!=null){
                str = str.replaceAll("\\.", "-");
                //减少冗余，具体的处理细节后面可以再去改。
                String [] Larr = str.split("\\s+");
                if(Larr[0]!=null){
                    Larr[0] = Larr[0].replaceAll("(?:AppOpenAction|\\[|\\])", "");
                }
//    			if(Larr[7]!=null){
//    				Larr[0] = Larr[0].replaceAll("(?:android|google|com|\\.|\\[|\\])", "");
//    			}

                //8个last合成一个字符
                for(String last:Larr){
                    word.append(last+"+");
                }
                //去除最后一个+号
                word.deleteCharAt(word.length() - 1);
                try {
                    fw = new FileWriter(resource,true);
                    word.append("\r\n");
                    fw.write(word.toString());
                    fw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                    System.out.println("写入失败");
                    System.exit(-1);
                }
                word.setLength(0);
            }

            String fwlasts = lasts.replaceAll("\\.", "-");

            // xie ru last
            for (int i=0;i<4;i++){
                try {
                    fw = new FileWriter(resource,true);
                    fw.write(fwlasts+"\r\n");
                    fw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                    System.out.println("写入失败");
                    System.exit(-1);
                }

            }

        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
        return resource;
    }
    /**
     * 用于处理词向量的字，把他全部替换为app名字
     * @param vector 词向量的路径
     */
    public static String dealResult(String vector){
        String str=null;
        String str1=null;
        String str2=null;
        //词向量的文件
        File file = new File(vector);
        StringBuffer word = new StringBuffer("");
        String resource = "/root/IdeaProjects/finish/word2vec/vector2.txt";
        FileWriter fw = null;
        //清空文件
        File d = new File(resource);
        if(d.exists())
            d.delete();
        int line = 0;
        try{

            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            while((str=br.readLine())!=null){
                String Key = str.substring(0, str.indexOf(" "));
                Key = Key.replaceAll("-", "\\.");
                String last = Key.substring(0,Key.lastIndexOf("+"));
                String appName = Key.substring(Key.lastIndexOf("+")+1);


                if(Key.equals(lasts)){
                    index = line;
                    System.out.println(index);
                }

                str2 = str.substring(str.indexOf(" "));
                word.append(appName);
                word.append(str2);


                try {
                    fw = new FileWriter(resource,true);
                    word.append("\r\n");
                    fw.write(word.toString());
                    fw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                    System.out.println("写入失败");
                    System.exit(-1);
                }
                word.setLength(0);
                line ++ ;
            }



        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
        return resource;
    }
    /**
     * 处理的第三步，改变科学计数法，以及app名字转id
     * @param vector 词向量的路径
     */
    public static String dealResult2(String src){
        String str;
        id2Name = new HashMap<String, Integer>();
//    	ArrayList<String> appId = new ArrayList<>();
        String[] split;
        //词向量的文件
        File file = new File(src);
        StringBuffer word = new StringBuffer("");
        String resource = "/root/IdeaProjects/finish/word2vec/predict.txt";
        FileWriter fw = null;
        //清空文件
        File d = new File(resource);
        if(d.exists())
            d.delete();

        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            int line = 0;
            while((str=br.readLine())!=null){
                split = str.split(" ");
                int i = 0;
                for(String temp : split){
                    // 把名字转换为id
                    if(i==0){
                        if(id2Name.get(temp) == null){
                            id2Name.put(temp, id2Name.size());
                        }

                        word.append(id2Name.get(temp));
                        i=1;
                    }else{
                        // 把词向量科学计数法转换
                        double vector = Double.parseDouble(temp);
                        DecimalFormat df = new java.text.DecimalFormat("0.000000000");
                        // 转换为9位小数
    					vector = (double)Math.round(vector*1000000000)/1000000000;
    					BigDecimal bigDecimal = new BigDecimal(vector);
    					word.append(" "+bigDecimal.toString());
                        word.append(" "+df.format(vector));
                    }
                }

                try {
                    fw = new FileWriter(resource,true);
                    word.append("\r\n");
                    fw.write(word.toString());
                    fw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                    System.out.println("写入失败");
                    System.exit(-1);
                }
                word.setLength(0);
                line++;
            }

        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
        return resource;
    }
    /**
     * 输入一个文件来计算词向量
     * @param filePath 计算上下文的段落文件
     * @return 计算后的词向量文件，每行第一个字符是词向量，之后的都是坐标值，以空格隔开
     * @throws IOException
     */
    public static String computeWord2VecAndComputeSimilarity(String filePath) throws IOException {
        // Strip white space before and after for each line
        SentenceIterator iter = new BasicLineIterator(filePath);
        // Split on white spaces in the line to get words
        TokenizerFactory t = new DefaultTokenizerFactory();
        String vector="/root/IdeaProjects/finish/word2vec/vector.txt";
        t.setTokenPreProcessor(new CommonPreprocessor());

        vec = new Word2Vec.Builder()
                .minWordFrequency(3)
                .iterations(1)
                .layerSize(100)
                .seed(42)
                .windowSize(5)
                .iterate(iter)
                .tokenizerFactory(t)
                .build();

        vec.fit();

        WordVectorSerializer.writeWordVectors(vec, vector);


        return vector;
    }

    public static void iknnm(int index){
        Process proc;
        String command = "python /root/IdeaProjects/finish/src/iknnm/main.py "+index;

        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            proc = Runtime.getRuntime().exec(command);
            is = proc.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            proc.waitFor();
        } catch (Exception e) {
            if(e instanceof InterruptedException  || e instanceof IOException ) {
                e.printStackTrace();
            }
        } finally {
//            IoUtils.close(br);
//            IoUtils.close(isr);
//            IoUtils.close(is);
        }

    }

    public static String[] getPredictApp(){
        // result appid
        String src = "/root/IdeaProjects/finish/data/pResultId.txt";
        String str,split[];
        File file = new File(src);
        String[] predictApp = new String[5];
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            while((str=br.readLine())!=null){
                split = str.split(" ");
                int i = 0;
                for(String temp : split){
                    for(String getKey: id2Name.keySet()){
                        if(id2Name.get(getKey) == stringToInt(temp)){
                            predictApp[i] = getKey;
                            System.out.println(getKey);
                        }
                    }
                    i++;
                }
            }
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
        return predictApp;
    }

    public static int stringToInt(String str){
        int a = 0;
        try {
            a = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return a;
    }
}
